﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;

namespace Utilities.Redis
{
    public class RedisConnectionHelp
    {
        //public static readonly string redisConnStr = ConfigurationCenter.Global["RedisConnectionString"];
        public static readonly string redisConnStr =WebConfigurationManager.AppSettings["RedisConnectionString"];

        private static readonly object locker = new object();
        public static readonly string SysCustomKeyPrex = "hx:EPS_:";
        private static ConnectionMultiplexer instance;

        /// <summary>
        /// 单例获取
        /// </summary>
        public static ConnectionMultiplexer Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null || !instance.IsConnected)
                        {
                            instance = GetManager();
                            if (instance == null) {
                               
                            }
                        }
                    }
                }
                return instance;
            }
        }

        public static ConnectionMultiplexer GetManager(string connectionString = null)
        {
            var connStr = connectionString ?? redisConnStr;
            ConnectionMultiplexer connect = null;
            try
            {
                 connect = ConnectionMultiplexer.Connect(connStr);
              
            }
            catch (Exception)
            {

                Console.WriteLine("Redis服务器已关闭");
                Console.ReadLine();



            }
            return connect;

        }

    }
}